package com.stroke.core;

import com.stroke.graphics.Animation;
import com.stroke.resources.GraphicResources;
import com.stroke.tools.animation.AnimationFile;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.File;
import java.io.FileInputStream;

/**
 * Author: Igor Bubelov
 * Date: 3/13/11 8:47 PM
 */
public class ResourcesParser {
    public static void parse(File file) {
        XMLReader reader;

        try {
            reader = XMLReaderFactory.createXMLReader();
        } catch (SAXException ex) {
            Logger.getLogger(ResourcesParser.class).error("Failed to create XML reader", ex);
            return;
        }

        DefaultHandler handler = new DefaultHandler() {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if (qName.equals("resources")) {
                    String graphicsPath = attributes.getValue("graphics");
                    GraphicResources.load(new File(graphicsPath));
                }
            }
        };

        reader.setContentHandler(handler);

        try {
            reader.parse(new InputSource(new FileInputStream(file)));
        } catch (Exception ex) {
            Logger.getLogger(ResourcesParser.class).error("Failed to parse resources configuration", ex);
        }
    }
}
